/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.NhanVienDao;
import POJO.NhanVien;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Khôi
 */
public class frDangNhap extends javax.swing.JInternalFrame {

	/**
	 * Creates new form frDangNhap
	 */
	public frDangNhap() {
		initComponents();
		this.setLocation(100, 100);

	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jLabel1 = new javax.swing.JLabel();
              jPanel1 = new javax.swing.JPanel();
              jLabel2 = new javax.swing.JLabel();
              txUser = new javax.swing.JTextField();
              jLabel3 = new javax.swing.JLabel();
              btnLogin = new javax.swing.JButton();
              txPass = new javax.swing.JPasswordField();

              setClosable(true);
              setIconifiable(true);

              jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
              jLabel1.setForeground(new java.awt.Color(0, 51, 255));
              jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
              jLabel1.setText("Login");

              jLabel2.setText("Username");

              jLabel3.setText("Password");

              btnLogin.setText("Login");
              btnLogin.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btnLoginActionPerformed(evt);
                     }
              });

              org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                     .add(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(100, Short.MAX_VALUE)
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                   .add(jLabel2)
                                   .add(jLabel3))
                            .add(33, 33, 33)
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                   .add(txUser, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                                   .add(txPass))
                            .addContainerGap(55, Short.MAX_VALUE))
                     .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(btnLogin)
                            .add(98, 98, 98))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                     .add(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                   .add(jLabel2)
                                   .add(txUser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                   .add(jLabel3)
                                   .add(txPass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(18, 18, 18)
                            .add(btnLogin)
                            .addContainerGap(39, Short.MAX_VALUE))
              );

              org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                     .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                     .add(layout.createSequentialGroup()
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(0, 0, 0)
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(0, 8, Short.MAX_VALUE))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

	private void khoaTatCa() {
		MainForm.mnNVK.setVisible(false);
		MainForm.mnNVGH.setVisible(false);
		MainForm.mnNVK.setVisible(false);
		MainForm.mnNVQL.setVisible(false);

	}

	private boolean dangNhap() {
		khoaTatCa();
		boolean flag = false;
		if (Var.NV.getLoaiNV() == 1) {
			MainForm.mnNVK.setVisible(true);
			flag = true;
		}
		if (Var.NV.getLoaiNV() == 2) {
			MainForm.mnNVBH.setVisible(true);
			flag = true;

		}
		if (Var.NV.getLoaiNV() == 3) {
			MainForm.mnNVGH.setVisible(true);
			flag = true;
		}
		if (Var.NV.getLoaiNV() == 4) {
			MainForm.mnNVQL.setVisible(true);
			flag = true;
		}
		if (flag) {
			MainForm.mLogIn.setVisible(false);
			MainForm.mLogOut.setVisible(true);
			MainForm.mThayDoiThongTinCaNhan.setVisible(true);

		}
		return flag;

	}
       private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
		// TODO add your handling code here:

		NhanVien nv = new NhanVien();
		NhanVien nv2 = new NhanVien();
		nv.setUserName(txUser.getText().trim());
		nv.setPassWord(txPass.getText().trim());
		NhanVienDao dao = new NhanVienDao();
		try {
			nv2 = dao.getByUsername(nv.getUserName());

		} catch (Exception e) {
		}

		if (nv2 != null && nv.getPassWord().equals(nv2.getPassWord()) && nv2.getTrangThai() == 1) {
			JOptionPane.showMessageDialog(this, "Đăng nhập thành công.");
			Var.NV = new NhanVien(nv2);
			if (dangNhap()) {
				this.dispose();
			}
		} else {
			JOptionPane.showMessageDialog(this, "Đăng nhập thất bại .");
		}
       }//GEN-LAST:event_btnLoginActionPerformed
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton btnLogin;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPasswordField txPass;
       private javax.swing.JTextField txUser;
       // End of variables declaration//GEN-END:variables
}
